package org.wangbao.web;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wangbao.model.News;
import org.wangbao.service.ContentParserService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class ContentParserAction extends ActionSupport implements
		ApplicationAware {

	final Logger log = LoggerFactory.getLogger(ContentParserAction.class);

	private String url = "http://news.baidu.com";

	public String content;

	private HashMap<String, News> conMap;

	private String keyword, sensitiveword;

	Map application;

	// 标识具体新闻的uuid
	private String index;

	private String xmlPath;

	private String imgPath;

	private String editPath;

	private String swfPath;

	private String journalId = "20100605";

	private String versionId = "A01";

	private String userid = "tester";

	// 直接返回带uuid的HashMap ，存储新闻列表；

	// 返回Block对象的json形式，包括 blockId,versionId,journalId;
	private String block;

	public ContentParserService contentParserService;

	public String editorWindow() {

		// swfPath = "gen.swf?xmlURL=" + xmlPath + "&imgURL=" +
		// imgPath+"&journalId="+journalId+

		swfPath = "gen.swf?xmlURL=" + "A7FMRD504C.xml" + "&imgURL="
				+ "A7FMRD504C.jpg" + "&journalId=" + journalId + "&versionId="
				+ versionId;
		return "pic";
		// http://localhost:9280/gen.swf?xmlURL=A7FMRD504C.xml&imgURL=A7FMRD504C.jpg
		// http://url/gen.swf?xmlURL=A07/A7FMRD504C.xml&imgURL=A07/A7FMRD504C.jpg
		// xmlURL:xml路径
		// imgURL:图片路径
		// 路径是绝对路径，或者是相对于swf文件所在页面的相对路径
	}

	public String execute() throws Exception {

		log.info("keyword is:" + keyword);

		// BASE64Decoder
		if (keyword == null) {
			return "init";
		}
		keyword = URLEncoder.encode(keyword, "GBK");
		log.info("trans keyword is:" + keyword);

		// 调整为HashMap的返回
		conMap = contentParserService.getNewsTitle(url, keyword);

		//HttpSession session = ServletActionContext.getRequest().getSession();
		//session.setAttribute(userid, conMap);
		application.put(userid, conMap);

		return "success";
	}

	public String detail() throws Exception {

		//HttpSession session = ServletActionContext.getRequest().getSession();
		//conMap = (HashMap<String, News>) session.getAttribute(userid);
		conMap = (HashMap<String, News>) application.get(userid);
		if (conMap.containsKey(index)) {
			News news = (News) conMap.get(index);
			contentParserService.getNewsContent(news);
			conMap.put(index, news);
			//session.setAttribute(userid, conMap);
			application.put(userid, conMap);
		} else {
			throw new RuntimeException("not include this index.");
		}
		return "success";
	}

	/**
	 * According to the id group submit, save and index the block news
	 * information 相对应的查询放置在buildFlash中
	 * 
	 * @return
	 */
	public void saveBlock() {

		HttpSession session = ServletActionContext.getRequest().getSession();
		conMap = (HashMap<String, News>) session.getAttribute(userid);
		contentParserService.saveBlock(block, conMap);

	}

	public String buildSensitiveWord() {
		return "sensitive";
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@JSON(serialize = false)
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	@JSON(serialize = false)
	public ContentParserService getContentParserService() {
		return contentParserService;
	}

	public void setContentParserService(
			ContentParserService contentParserService) {
		this.contentParserService = contentParserService;
	}

	public String getSensitiveword() {
		return sensitiveword;
	}

	public void setSensitiveword(String sensitiveword) {
		this.sensitiveword = sensitiveword;
	}

	public String getIdGroup() {
		return block;
	}

	public void setIdGroup(String idGroup) {
		this.block = idGroup;
	}

	public String getBlock() {
		return block;
	}

	public void setBlock(String block) {
		this.block = block;
	}

	public String getXmlPath() {
		return xmlPath;
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public String getEditPath() {
		return editPath;
	}

	public void setEditPath(String editPath) {
		this.editPath = editPath;
	}

	public HashMap<String, News> getConMap() {
		return conMap;
	}

	public String getSwfPath() {
		return swfPath;
	}

	public void setSwfPath(String swfPath) {
		this.swfPath = swfPath;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public void setConMap(HashMap<String, News> conMap) {
		this.conMap = conMap;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public String getJournalId() {
		return journalId;
	}

	public void setJournalId(String journalId) {
		this.journalId = journalId;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

}
